package com.qianbei.user.income;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Request;
import com.bumptech.glide.Glide;
import com.qianbei.R;
import com.qianbei.common.base.BaseActivity;
import com.qianbei.common.init.SetData;
import com.qianbei.common.net.control.ServerControl;
import com.qianbei.common.net.control.ServerResult;
import com.qianbei.common.net.view.MsgAlert;
import com.qianbei.common.rxbus.RxBus;
import com.qianbei.share.ui.ShareFragmentDialog;
import com.umeng.socialize.UMShareAPI;

import static com.qianbei.common.init.SetData.getCareer;
import static com.qianbei.common.init.SetData.getFace;

public class ShareIncomeActivity extends BaseActivity implements View.OnClickListener, ShareFragmentDialog.OnShareSuccess {

    private TextView mTvShareMoney;
    private ImageView mIvShareQrcode;
    private Button mBtInviationCode;
    private String userID;
    private ScrollView mScShareIncome;
    private LinearLayout mLlNoEmptyView;
    private ImageView mIvNoNetWork;
    private String fenxiangimg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_incomr);
        userID = SetData.getUserID();

        setTitle("晒晒收入");
        initView();
        initGetData();
    }

    public void initView() {
        mTvShareMoney = (TextView) findViewById(R.id.tv_share_money);
        mIvShareQrcode = (ImageView) findViewById(R.id.iv_share_qrcode);
        mBtInviationCode = (Button) findViewById(R.id.bt_inviation_code);
        mScShareIncome = (ScrollView) findViewById(R.id.sv_share_income);
        mLlNoEmptyView = (LinearLayout) findViewById(R.id.ll_no_emptyview);
        mIvNoNetWork = (ImageView) findViewById(R.id.iv_no_network);

        mBtInviationCode.setOnClickListener(this);
        mIvNoNetWork.setOnClickListener(this);
    }

    private void initGetData() {
        ServerControl sc = new ServerControl(Request.Method.GET, SHARE_INCOME + userID + "?");

        sc.serverListener = new ServerControl.ServerListener() {
            @Override
            public void serverFinish(ServerResult sr) {
                if (sr.isContinue) {
                    mScShareIncome.setVisibility(View.VISIBLE);
                    mLlNoEmptyView.setVisibility(View.GONE);

                    ShareIncomeBean shareIncomeBean = JSON.parseObject(sr.bodyData.toString(), ShareIncomeBean.class);

                    String codeimg = shareIncomeBean.getData().getCodeimg();
                    fenxiangimg = shareIncomeBean.getData().getFenxiangimg();
                    double money = shareIncomeBean.getData().getMoney();

                    mTvShareMoney.setText(money + "元");
                    if(!((BaseActivity)mContext).isFinishing()){
                        Glide.with(mContext)
                                .load(codeimg)
                                .into(mIvShareQrcode);
                    }

                } else {
                    mScShareIncome.setVisibility(View.GONE);
                    mLlNoEmptyView.setVisibility(View.VISIBLE);
                }
            }
        };
        sc.startVolley();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_inviation_code:
                if (!TextUtils.isEmpty(fenxiangimg)) {
                    ShareFragmentDialog dialogs = ShareFragmentDialog.newInstance("", "", "", fenxiangimg, "img", false);
                    dialogs.show(getFragmentManager(), this.getClass().toString());
                    dialogs.setOnShareSuccess(ShareIncomeActivity.this);
                } else {
                    new MsgAlert().show("未获取到分享链接");
                }

                break;
            case R.id.iv_no_network:
                initGetData();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public boolean OnShareSuccess(boolean isShare) {
        if (isShare) {
            ServerControl sc = new ServerControl(Request.Method.GET, SHAREINCOM_TASK + SetData.getUserID() + "?");

            sc.serverListener = new ServerControl.ServerListener() {
                @Override
                public void serverFinish(ServerResult sr) {
                    if (sr.isContinue) {
                        RxBus.getDefault().post("userinfo");
                    }
                }
            };
            sc.startVolley();
        }
        return false;
    }
}
